/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ml.main;

import com.ml.training.TrainingHelper;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonModel;
import javax.swing.JFileChooser;

/**
 *
 * @author TUPEO
 */
public class Training extends javax.swing.JPanel {

    JFileChooser chooser;
    File datasetFile, saveFile;
    TrainingHelper trainingHelper;
    
    public Training() {
        initComponents();
        chooser=new JFileChooser();
        pathDatasetButton.addActionListener(new OPen());
        saveTo.addActionListener(new Save());
        
        continousValueGroup.add(continousValue1);
        continousValueGroup.add(continousValue2);
        
        missingValueGroup.add(popular1);
        missingValueGroup.add(popular2);
        missingValueGroup.add(probability);
        
    }

    class OPen implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            int returnVal = chooser.showOpenDialog(Training.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                datasetFile = chooser.getSelectedFile();
                pathLabel.setText(datasetFile.getPath().toString());
            } else {
                
            }
            
        }
        
    }
    class Save implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            int returnVal = chooser.showSaveDialog(Training.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                saveFile = chooser.getSelectedFile();
            } else {
                
            }
            
        }
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        continousValueGroup = new javax.swing.ButtonGroup();
        missingValueGroup = new javax.swing.ButtonGroup();
        pathLabel = new javax.swing.JTextField();
        pathDatasetButton = new javax.swing.JButton();
        overFitting = new javax.swing.JCheckBox();
        popular1 = new javax.swing.JRadioButton();
        popular2 = new javax.swing.JRadioButton();
        probability = new javax.swing.JRadioButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        start = new javax.swing.JButton();
        missingValue = new javax.swing.JCheckBox();
        saveTo = new javax.swing.JButton();
        continousValue1 = new javax.swing.JRadioButton();
        continousValue2 = new javax.swing.JRadioButton();

        pathDatasetButton.setText("dataset");
        pathDatasetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathDatasetButtonActionPerformed(evt);
            }
        });

        overFitting.setSelected(true);
        overFitting.setText("solve over fitting");

        popular1.setSelected(true);
        popular1.setText("popular-1");
        popular1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popular1ActionPerformed(evt);
            }
        });

        popular2.setText("popular-2");
        popular2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popular2ActionPerformed(evt);
            }
        });

        probability.setText("probability");

        start.setText("start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        missingValue.setSelected(true);
        missingValue.setText("fill mising value");
        missingValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                missingValueActionPerformed(evt);
            }
        });

        saveTo.setText("save to");
        saveTo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveToActionPerformed(evt);
            }
        });

        continousValue1.setSelected(true);
        continousValue1.setText("continous value-1");

        continousValue2.setText("continous value-2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pathLabel, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(missingValue)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(continousValue2))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(overFitting)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                                .addComponent(continousValue1)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(popular1)
                            .addComponent(popular2)
                            .addComponent(probability))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(start, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pathDatasetButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(saveTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pathLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pathDatasetButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(popular1)
                    .addComponent(overFitting)
                    .addComponent(continousValue1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(popular2)
                    .addComponent(missingValue)
                    .addComponent(saveTo)
                    .addComponent(continousValue2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(probability)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(start, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void popular2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popular2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_popular2ActionPerformed

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        try {
            // TODO add your handling code here:
            ButtonModel buttonModel=missingValueGroup.getSelection();
            int popular=TrainingHelper.POPULAR1;
            if(buttonModel.equals(popular1.getModel())) popular=TrainingHelper.POPULAR1;
            else if(buttonModel.equals(popular2.getModel())) popular=TrainingHelper.POPULAR2;
            else if(buttonModel.equals(probability.getModel())) popular=TrainingHelper.PROBABILITY;
            
            ButtonModel model=continousValueGroup.getSelection();
            int continous=TrainingHelper.CONTINOUS_VALUE1;
            if(model.equals(continousValue1.getModel())) continous=TrainingHelper.CONTINOUS_VALUE1;
            else if(model.equals(continousValue2.getModel())) continous=TrainingHelper.CONTINOUS_VALUE2;
            
            System.out.println(popular+"^&^&^&"+continous);
            TrainingHelper trainingHelper=new TrainingHelper(overFitting.isSelected(), missingValue.isSelected(),popular, continous, datasetFile, saveFile);
            trainingHelper.saveToFile();
        } catch (IOException ex) {
            Logger.getLogger(Training.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_startActionPerformed

    private void pathDatasetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathDatasetButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pathDatasetButtonActionPerformed

    private void popular1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popular1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_popular1ActionPerformed

    private void missingValueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_missingValueActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_missingValueActionPerformed

    private void saveToActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveToActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_saveToActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton continousValue1;
    private javax.swing.JRadioButton continousValue2;
    private javax.swing.ButtonGroup continousValueGroup;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JCheckBox missingValue;
    private javax.swing.ButtonGroup missingValueGroup;
    private javax.swing.JCheckBox overFitting;
    private javax.swing.JButton pathDatasetButton;
    private javax.swing.JTextField pathLabel;
    private javax.swing.JRadioButton popular1;
    private javax.swing.JRadioButton popular2;
    private javax.swing.JRadioButton probability;
    private javax.swing.JButton saveTo;
    private javax.swing.JButton start;
    // End of variables declaration//GEN-END:variables
}
